//
//  ServerRequest.swift
//  DianShangWen
//
//  Created by 周文轩 on 15/4/1.
//  Copyright (c) 2015年 &#21608;&#25991;&#36713;. All rights reserved.
//

import Foundation
import Alamofire
import Argo

class ServerRequest {
    class func ServerRequest(APIName: String, requestStayle: String, params: NSDictionary, tag: Int, closure: (dataDictionary: NSDictionary) -> Void) {
        
        let parameters = Parameters.parameterRequest(APIName, requestStayle: requestStayle, params: params)
        
        let data = NSJSONSerialization.dataWithJSONObject(parameters, options: nil, error: nil)
        let parameter = NSString(data: data!, encoding: NSUTF8StringEncoding)
        if requestStayle == ".post" {
            postRequest(parameter!, tag: tag).responseJSON {
                (_,_,JSON,error) -> Void in
                switch tag {
                case 10:
                    let value = JSONValue.parse(JSON!)
                    let userLogin = UserLogin.decode(value)
                    closure(dataDictionary: ["success": userLogin!.successcount])
                default:
                    println(error)
                }
            }
        }
        else if requestStayle == ".get"
        {
            getRequest(parameter!, tag: tag).responseJSON { (_, _, JSON, error) -> Void in
                switch tag {
                case 10:
                    let value = JSONValue.parse(JSON!)
                    let userLogin = UserLogin.decode(value)
                    closure(dataDictionary: ["string": userLogin!.msg])
                case 11:
                    println("注册:\(JSON)")
                case 12:
                    let jsonCurriculum = JSONAnalysis.finishSameJSONAnalysis(JSON! as! NSDictionary) as NSDictionary
                    closure(dataDictionary: jsonCurriculum)
                case 13:
                    println(JSON!)
                default:
                    println("error")
                }
            }
        }
    }
    
    class func postRequest(parameter: NSString, tag: Int) -> Request {
        return Alamofire.request(.POST, "http://api.dianshangwen.net/api", parameters: ["c": parameter])
    }
    
    class func getRequest(parameter: NSString, tag: Int) -> Request {
        return Alamofire.request(.GET, "http://api.dianshangwen.net/api", parameters: ["c": parameter])
    }
}

